import importlib
import os
import sys

pathlist = os.path.dirname(os.path.abspath(__file__)).split(os.sep)
root_path = os.sep.join(pathlist[:pathlist.index("re-common") + 1])
sys.path.insert(0, root_path)
sys.path = list(set(sys.path))
from re_common.baselibrary.utils.importfun import import_model


def test_importlib():
    """
    pytest importlib_test.py::test_importlib -s
    测试导入包　但import_module只能导入到文件级别　像import 而无法像from ... import ..
    :return:
    """
    step1_test = import_model("step1_test")
    a = step1_test.A()
    a.prints()


def test_importlib_more():
    """
    测试导入包　但import_module只能导入到文件级别　像import 而无法像from ... import ..
    :return:
    """
    step1_test = import_model("test1.step1_test")
    print(step1_test)
    a = step1_test.A()
    a.prints()



def test_import_to_val():
    """
    这个测试不能用ｐｙｔｅｓｔ
    而是直接调用函数
    pytest importlib_test.py::test_import_to_val -s
    :return:
    """
    step1_test = importlib.import_module("step1_test")
    from re_common.baselibrary.utils.importfun import import_to_val
    import_to_val(step1_test, ["A", "main", "prints"], globals(), locals())
    A().prints()
    prints()
    main()


# 测试test_import_to_val使用直接调用的方式
test_import_to_val()


class TestImportlibFun(object):

    def __init__(self):
        step1_test = importlib.import_module("step1_test")
        from re_common.baselibrary.utils.importfun import import_fun
        import_fun(self, step1_test, ["A"])
        self.a = self.A()
        print(self.a.prints())
        print("************")


# 测试TestImportlibFun使用直接调用的方式
# TestImportlibFun()
